package com.jml.防抓包篡改.controller;

import com.jml.APIOauth2.base.BaseApiService;
import com.jml.APIOauth2.base.ResponseBase;
import com.jml.API接口幂等性.utils.BaseRedisService;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PayController extends BaseApiService {
	@Autowired
	private BaseRedisService baseRedisService;
	private static long timeToken = 15 * 60l;

	//客户端传递token
	//该服务器根据token去redis中获取参数，进行业务操作
	@RequestMapping("/pay")
	public ResponseBase pay(String token) {
		// 获取提交参数 数据库保存.,
		if (StringUtils.isEmpty(token)) {
			return setResultError("token 不能为空！");
		}
		String reuslt = (String) baseRedisService.getString(token);
		if (StringUtils.isEmpty(reuslt)) {
			return setResultError("参数不能空!");
		}
		System.out.println("获取提交的参数reuslt：" + reuslt);
		return setResultSuccess("获取提交的参数reuslt：" + reuslt);
	}

	//服务器后台之间的通信
	//该服务器获取具体参数,保存在该服务器的redis中，返给客户端一个token
	@RequestMapping("/getPayToken")
	public String pay(Long userId, Long money) {
		String payToken = UUID.randomUUID().toString();
		baseRedisService.setString(payToken, userId + "-" + money, timeToken);
		return payToken;
	}
}

